package com.ph.util.pagination;

import java.util.List;

import com.ph.irp.dto.PageDTO;

public final class PageUtil {
	private static int PAGE_SIZE = 10;

	public static PageInfo pageOperation(String turnPage, List list) {
		PageInfo pageInfo = null;
		
		if (turnPage != null) {
			int value = Integer.parseInt(turnPage);
			if ((value - 1) * PAGE_SIZE >=list.size()) {
				value--;
			}
			
			
			pageInfo = new PageInfo((value - 1) * PAGE_SIZE, PAGE_SIZE, list
					.size());
		} else {
			pageInfo = new PageInfo(0, PAGE_SIZE, list.size());
		}

		return pageInfo;
	}
	
	public static PageDTO generatePageDTO(List list, String url,String cpage) {
		PageDTO page = new PageDTO();
		PageInfo pageInfo = pageOperation(cpage, list);
		page.setTotal(String.valueOf(pageInfo.getTotalPage()));
		page.setCpage(String.valueOf(pageInfo.getCurrentPage()));
		page.setResList(list.subList(pageInfo.getStartIndex(), pageInfo
				.getEndIndex() + 1));
		page.setUrl(url);

		return page;
	}
}
